﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using MarchEstate.Manager.Dao.Carport;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Carport
{
    public class CarportManager : AbstractCarport, IJoinManager
    {
        private CarDao carDao = null;

        public CarportManager()
        {
        }

        public override void InitDao()
        {
            base.InitDao();
            carDao = NewDao<CarDao>();
        }

        public override bool Save()
        {
            //Guid转换
            if (this.CarPortId == null || this.CarPortId == "")
            {
                if (carDao.ExistCarportModel(Code))
                {
                    throw new Exception("输入的代码已存在，请重新输入！");
                }
                this.CarPortId = null;
                return carDao.AddCarport(this);
            }
            else
            {
                return carDao.UpdateCarport(this);
            }
        }

        public override bool Delete(string _carPortId)
        {
            return carDao.DeleteCarport(_carPortId);
        }

        public override AbstractCarport GetCarport(string _code)
        {
            throw new NotImplementedException();
        }

        public override List<AbstractCarport> GetCarportList(string _carAreaCode)
        {
            return carDao.GetCarportList(_carAreaCode);
        }

        #region IJoinManager 成员
        [AOP(typeof(AopTransaction))]
        public bool Join(System.Collections.Hashtable value)
        {
            //如果不是选择的人员则新增人员
            AbstractOwner owner = NewObject<AbstractOwner>();
            if (value["ownerId"].ToString().Trim() == "")
            {
                owner.Name = value["OwnerName"].ToString();
                owner.OftenMobile = value["tel"].ToString();
                owner.save();
                value["ownerId"] = owner.OwnerId;
            }

            carDao.JoinCarport(value["srccId"].ToString(), value["ownerId"].ToString(), value["OwnerName"].ToString(), value["roomnumber"].ToString(), value["CarNumber"].ToString(), value["Carbrand"].ToString());

            //添加项目对应关系表
            AbstractServiceItem item = NewObject<AbstractServiceItem>();
            AbstractServiceItem _item = item.GetServiceItem(value["itemcode"].ToString());
            AbstractOSRCCItemRelation _srcc = NewObject<AbstractOSRCCItemRelation>();
            _srcc.OwnerId = owner.OwnerId;
            _srcc.SrccId = value["srccId"].ToString();
            _srcc.ItemCode = _item.Code;
            _srcc.ItemName = _item.ItemName;
            //如果绑定的项目已存在就不增加重复的
            _srcc.Delete(value["srccId"].ToString(), null);
            _srcc.save();
            //初始化默认值
            _srcc.UpdateThisFee(_srcc.SrccId, _srcc.ItemCode, Convert.ToDateTime(value["maturity"]).Date, Convert.ToDateTime(value["maturity"]).Date, Convert.ToDecimal(value["Lastsurplus"]), Convert.ToDecimal(value["LastLateFee"]));
            return true;
        }
        [AOP(typeof(AopTransaction))]
        public bool CancelJoin(string srccId)
        {
            AbstractOSRCCItemRelation _srcc = NewObject<AbstractOSRCCItemRelation>();
            _srcc.Delete(srccId,null);
            return carDao.CancelJoin(srccId);
        }

        #endregion
    }
}
